package com.github.wui.VerticalSeekBar;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.github.wui.R;

/**
 * 垂直滑动条
 *
 * @author <a href="https://github.com/karai-oss">Mr.xie</a>
 * @Date 2025/5/6
 */
public class VerticalSeekBar extends View {


    enum Dir {
        T_B, B_T
    }

    private int defaultHeight = 400;
    private int defaultWidth = 30;


    /**
     * 方向
     */
    private int dir = Dir.B_T.ordinal();
    /**
     * 轨道画笔
     */
    private Paint trackPaint;


    /**
     * 轨道是否是圆头
     */
    private boolean trackIsRound = true;


    /**
     * 滑块是否为圆角 默认不是圆角
     */
    private boolean sliderIsRound = false;


    /**
     * 滑块覆盖路径 是否为圆角 默认不是圆角
     */
    private boolean sliderPathIsRound = false;


    /**
     * 轨道颜色
     */
    private int trackColor = Color.parseColor("#e6e6e6");


    /**
     * 轨道宽度
     */
    private int trackWidth = 20;
    /**
     * 滑块路径的画笔
     */
    private Paint sliderPathPaint = null;


    /**
     * 滑块覆盖路径的宽度
     */
    private float sliderPathWidth = 20f;

    /**
     * 滑块路径的颜色
     */
    private int sliderPathColor = Color.parseColor("#d2b116");


    /**
     * 滑块的画笔
     */
    private Paint sliderPaint = null;


    /**
     * 滑块宽度
     */
    private int sliderWidth = 80;


    /**
     * 滑块高度
     */
    private int sliderHeight = 10;

    /**
     * 滑块的颜色
     */
    private int sliderColor = Color.parseColor("#d2b116");


    /**
     * 进度
     */
    private float progress = 0f;


    /**
     * 轨道距离组件顶部和底部的距离
     */
    private int trackSpaceDistance = 30;

    /**
     * 是否可以滑动 判定滑块区域
     */
    private boolean isStartSlider = false;


    /**
     * 记录 初始化的时候 滑块的位置
     */

    private int recordSlider_position = 0;

    /**
     * 用于控制 B_T 模式与底部的间距
     */
    private int sliderBlockSpaceDistance = 20;


    private Point sliderBockScanPoint = new Point();


    /**
     * 滑动速度
     */
    private int speed_rate = 4;

    public VerticalSeekBar(Context context) {
        this(context, null);
    }

    public VerticalSeekBar(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public VerticalSeekBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.VerticalSeekBar);

        this.trackColor = typedArray.getColor(R.styleable.VerticalSeekBar_trackColor, Color.parseColor("#e6e6e6"));
        this.sliderPathColor = typedArray.getColor(R.styleable.VerticalSeekBar_sliderPathColor, Color.parseColor("#d2b116"));
        this.sliderColor = typedArray.getColor(R.styleable.VerticalSeekBar_sliderBlockColor, Color.parseColor("#d2b116"));
        this.trackWidth = (int) typedArray.getDimension(R.styleable.VerticalSeekBar_trackWidth, 20f);
        this.sliderPathWidth = (int) typedArray.getDimension(R.styleable.VerticalSeekBar_sliderPathWidth, 20f);
        this.trackIsRound = typedArray.getBoolean(R.styleable.VerticalSeekBar_trackIsRound, false);
        this.sliderIsRound = typedArray.getBoolean(R.styleable.VerticalSeekBar_sliderIsRound, false);
        this.sliderPathIsRound = typedArray.getBoolean(R.styleable.VerticalSeekBar_sliderPathIsRound, false);
        this.sliderBottom = typedArray.getDimension(R.styleable.VerticalSeekBar_slider_position, 10f);
        this.recordSlider_position = (int) this.sliderBottom;
        this.sliderBlockSpaceDistance = (int) typedArray.getDimension(R.styleable.VerticalSeekBar_slider_distance_bottom, 10f);
        this.trackSpaceDistance = (int) typedArray.getDimension(R.styleable.VerticalSeekBar_track_distance_bottom, 30f);
        this.progress = typedArray.getInteger(R.styleable.VerticalSeekBar_progress, 0);
        this.sliderHeight = typedArray.getInteger(R.styleable.VerticalSeekBar_slider_block_Height, 0);
        this.speed_rate = typedArray.getInteger(R.styleable.VerticalSeekBar_speed_rate, 4);

        typedArray.recycle();
        init();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        ViewGroup.LayoutParams layoutParams = getLayoutParams();
        int width = layoutParams.width;
        int height = layoutParams.height;
        // 不能使用默认的 默认就是 200 ，100
        if (width == ViewGroup.LayoutParams.WRAP_CONTENT || width == ViewGroup.LayoutParams.MATCH_PARENT) {
            width = defaultWidth;
        }
        if (height == ViewGroup.LayoutParams.WRAP_CONTENT || height == ViewGroup.LayoutParams.MATCH_PARENT) {
            height = defaultHeight;
        }

        // 是自己指定的高度 那么就直接进行计算即可
        setMeasuredDimension(width, height);

    }

    private void init() {
        this.trackPaint = new Paint();
        this.trackPaint.setAntiAlias(true);
        this.trackPaint.setColor(trackColor);
        this.trackPaint.setStrokeCap(trackIsRound ? Paint.Cap.ROUND : Paint.Cap.SQUARE);
        this.trackPaint.setStrokeWidth(trackWidth);


        this.sliderPathPaint = new Paint();
        this.sliderPathPaint.setAntiAlias(true);
        this.sliderPathPaint.setColor(sliderPathColor);
        this.sliderPathPaint.setStrokeCap(sliderPathIsRound ? Paint.Cap.ROUND : Paint.Cap.SQUARE);


        // 初始化滑块
        this.sliderPaint = new Paint();
        this.sliderPaint.setAntiAlias(true);
        this.sliderPaint.setColor(sliderColor);

    }

    float sliderBottom = 0;

    @Override
    protected void onDraw(@NonNull Canvas canvas) {
        super.onDraw(canvas);
        initTrack(canvas);
        initSliderPath((int) progress, canvas);

        drawSlider(canvas, 0, getMeasuredWidth(), 0, progress);

    }


    /**
     * 从上往下滑动
     *
     * @param progress 进度
     * @param canvas   画布对象
     */
    private void initSliderPath(int progress, Canvas canvas) {
        if (dir == Dir.B_T.ordinal()) {
            canvas.drawLine(getMeasuredWidth() / 2, getMeasuredHeight() - progress - trackSpaceDistance, getMeasuredWidth() / 2, getMeasuredHeight() - trackSpaceDistance, this.sliderPathPaint);
        }
    }

    /**
     * 初始化轨道
     *
     * @param canvas 画布
     */
    private void initTrack(Canvas canvas) {
        canvas.drawLine(getMeasuredWidth() / 2, trackSpaceDistance, getMeasuredWidth() / 2, getMeasuredHeight() - trackSpaceDistance, this.trackPaint);
    }


    /**
     * 绘制滑块
     */
    private void drawSlider(Canvas canvas, float left, float right, float top, float bottom) {
        this.sliderPathPaint.setStrokeWidth(isStartSlider || progress != 0 ? sliderPathWidth : 0);
        RectF sliderRect = new RectF();
        if (dir == Dir.T_B.ordinal()) {
            sliderRect.left = left;
            sliderRect.right = right;
            sliderRect.top = top;
            sliderRect.bottom = bottom;
        } else {
            sliderRect.left = (float) getMeasuredWidth() / 2 - 20;
            sliderRect.right = getMeasuredWidth() / 2 + 20;
            // 30 shi
            sliderRect.top = getMeasuredHeight() - bottom - sliderBlockSpaceDistance - sliderHeight - sliderBottom;
            sliderRect.bottom = getMeasuredHeight() - sliderBlockSpaceDistance - bottom - sliderBottom;

        }

        sliderBockScanPoint.x = (int) sliderRect.top;
        sliderBockScanPoint.y = (int) (getMeasuredHeight() - bottom);

        canvas.drawRoundRect(sliderRect, sliderIsRound ? sliderRect.bottom - sliderRect.top : 0, sliderIsRound ? sliderRect.bottom - sliderRect.top : 0, sliderPaint);
    }


    float startX = 0;
    float startY = 0;

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                float x = event.getX();
                float y = event.getY();
                // x 就是top
                startX = x;
                startY = y;
//                if (y > sliderBockScanPoint.x && y < sliderBockScanPoint.y) { // 在区域内 可以滑动
//                    isStartSlider = true;
//
//                } else {
//                    isStartSlider = false;
//                }

                break;
            case MotionEvent.ACTION_MOVE:
                // 判断 网上还是 往下
                float endX = event.getX();
                float endy = event.getY();

//                Log.e("TAG", "onTouchEvent: " + endy  + " " + (getMeasuredHeight() - trackSpaceDistance * 3) + " " +( endy - startY > 0));
                if (endy - startY < 0) {// 往上

                    if (sliderBockScanPoint.x <= trackSpaceDistance || sliderBockScanPoint.x < 0) {
                        upSlider(true);
                    } else {
                        upSlider(false);
                    }

                } else if (endy - startY > 0) {
                    // 往下
                    if (sliderBockScanPoint.y >= getMeasuredHeight() - sliderBlockSpaceDistance) {
                        DownSlider(true);
                    } else {
                        DownSlider(false);
                    }

                }
                break;
        }

        return true;
    }


    /**
     * 下滑
     */
    private void DownSlider(boolean isMost) {
        if (isMost) {
            progress -= 0;
            sliderBottom = 0;
            isStartSlider = false;
        } else {
            progress -= speed_rate;
            sliderBottom = recordSlider_position;
            isStartSlider = true;
        }
        invalidate();

    }


    /**
     * 上划
     *
     * @param isMost 是否到达边界
     */
    private void upSlider(boolean isMost) {
        if (isMost) {
            progress += 0;
            sliderBottom = recordSlider_position + recordSlider_position / 2;
        } else {
            progress += speed_rate;
            sliderBottom = recordSlider_position;
            isStartSlider = true;
        }
        postInvalidateDelayed(10);


    }
}
